// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import MainGame from "../game/MainGame";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Guide extends cc.Component {

    public static instance: Guide;

    @property(cc.Node)
    m_spr: cc.Node = null;
    @property(cc.Node)
    m_mask: cc.Node = null;
    @property(cc.Node)
    m_tip: cc.Node = null;
    @property(cc.Label)
    m_tipLabel: cc.Label = null;
    @property(cc.Animation)
    m_head: cc.Animation = null;

    n_tipKey: number = 10001;
    n_mainGame: MainGame = null;

    onLoad() {
        Guide.instance = this;
        this.m_mask.width = cc.winSize.width;
        this.m_mask.height = cc.winSize.height;
        this.m_mask.on(cc.Node.EventType.TOUCH_END, this.touchMask, this);
        this.n_mainGame = this.node.parent.getChildByName("game").getComponent(MainGame);
    }
    /**
     * 显示引导界面 设置宽高和坐标
     * @param width  
     * @param heigth 
     * @param pos 
     */
    showGuide(width: number, heigth: number, pos: cc.Vec2, showHead: boolean) {
        this.node.active = true;
        this.m_spr.width = width + 10;
        this.m_spr.height = heigth + 10;
        this.m_spr.setPosition(pos);
        this.m_head.node.active = showHead;
        if (showHead) {
            this.m_head.node.setPosition(pos);
        }
        // this.m_spr.x = pos.x;
        // this.m_spr.y = pos.y;
        this.m_mask.x = -this.m_spr.x;
        this.m_mask.y = -this.m_spr.y;
        this.m_tipLabel.string = config.dataJson.guide[this.n_tipKey].guide;
        this.n_tipKey++;
    }

    hideGuide(initKey: boolean = false) {
        this.node.active = false;
        if(initKey){
            this.n_tipKey = 10001;
        }
    }

    touchMask() {
        if(this.n_tipKey == 10002){
            this.n_mainGame.guideSelectMaterial();
        }else{
            if(!config.guide){
                this.node.destroy();
                console.log(this.node.parent);
            }
        }
    }
}
